﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Power_edit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtId.Attributes.Add("readonly", "readonly");
            bd();
        }
    }
    void bd()
    {
        string id =Request["id"];
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        ddlPid.DataSource = dal.Power;
        ddlPid.DataBind();
        CMSModel.Power power = dal.Power.First(x => x.Id == id);
        txtId.Text = power.Id.ToString();
        txtName.Text = power.Name;
        ddlPid.SelectedValue = power.Pid.ToString();
        txtUrl.Text = power.Url;
        if (power.IsEnable == false)
            rbtnForbidden.Checked=true;
        else
            rbtnNormal.Checked = true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string id =txtId.Text.ToString();
        CMSModel.Power power = dal.Power.First(x => x.Id == id);
        power.Name = txtName.Text;
        power.Url = txtUrl.Text;
        power.Pid = ddlPid.SelectedValue;
        if (rbtnForbidden.Checked)
            power.IsEnable = false;
        else
            power.IsEnable = true;
        try
        {
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('修改成功'),document.location='Default.aspx'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "editPower", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('修改失败')", true);
        }
    }
}